import {
  defineStore
} from 'pinia'
import {
  ref
} from 'vue'

export const useAddress = defineStore('address', () => {


  const address = ref(null)
  const addressList = ref([])

  // 添加地址
  const addAddressList = (data) => {
    if (Object.prototype.toString.call(data) === '[object Array]') {
      addressList.value.push(...data)
    } else {
      addressList.value.push(data)
    }
  }

  // 编辑地址
  const editAddressList = (data) => {
    addressList.value = addressList.value.map((e, i) => {
      if (e.id == data.id) return data
      return e
    })
  }

  // 删除地址
  const deleteAddressList = (userId) => {
    addressList.value = addressList.value.filter(e => e.id != userId)
  }

  // 替换地址
  const replaceAddressList = (data) => {
    addressList.value = data
  }




  return {
    address, editAddressList, addressList, addAddressList, deleteAddressList, replaceAddressList
  }
},
  {

    // 打开持久化，默认进行 localStorage 储存
    // persist: true

    // 进行 sessionStorage 储存
    storage: sessionStorage,
  },
)